package com.yanq.liancar.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.gyf.barlibrary.ImmersionBar;
import com.vondear.rxtool.RxActivityTool;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.SmallCNewsActivity;
import com.yanq.liancar.activity.SmallCSearchActivity;
import com.yanq.liancar.activity.TrajectoryListActivity;
import com.yanq.liancar.activity.VehicleTrendsActivity;
import com.yanq.liancar.address_selection.CityPickerActivity;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.view.PieChartView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.app.Activity.RESULT_OK;

/**
 * A simple {@link Fragment} subclass.
 */
public class CHomeFragment extends BaseLYFragment {

    @BindView(R.id.tv_city_chome)
    TextView tvCity;
    @BindView(R.id.tv_search)
    TextView tvSearch;
    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.ll_top)
    LinearLayout llTop;
    @BindView(R.id.imageView2)
    ImageView imageView2;
    @BindView(R.id.pie_chart)
    PieChartView pieChart;
    @BindView(R.id.rl_status)
    LinearLayout rlStatus;
    @BindView(R.id.tv)
    TextView tv;
    @BindView(R.id.imageView3)
    ImageView imageView3;
    @BindView(R.id.textView7)
    TextView textView7;
    @BindView(R.id.img_catstatus)
    ImageView imgCatstatus;
    @BindView(R.id.textview0)
    TextView textview0;
    @BindView(R.id.textView8)
    TextView textView8;
    @BindView(R.id.ll_cardoctor)
    LinearLayout llCardoctor;
    @BindView(R.id.quick_1)
    TextView quick1;
    @BindView(R.id.rl_trajectory)
    RelativeLayout rl_trajectory;
@BindView(R.id.rl_VehicleTrends)
RelativeLayout rl_VehicleTrends;
    private View headView;
    private List<PieChartView.PieceDataHolder> mPieceDataHolders;
    //声明AMapLocationClient类对象
    private AMapLocationClient mLocationClient = null;
    //声明AMapLocationClientOption对象
    private AMapLocationClientOption mLocationOption = null;
    private String city, province, code;
    protected ImmersionBar mImmersionBar;

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_chome);

        ImmersionBar.setTitleBar(getActivity(), llTop);
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.keyboardEnable(true).navigationBarWithKitkatEnable(false).init();
        mImmersionBar.statusBarDarkFont(true).init();
        getLocation();
        initView();

    }

    private void initView() {
        mPieceDataHolders = new ArrayList<>();
        mPieceDataHolders.add(new PieChartView.PieceDataHolder(20, 0xFFF6A956, "离线（20）"));
        mPieceDataHolders.add(new PieChartView.PieceDataHolder(55, 0xFF5DABF0, "行驶（55）"));
        mPieceDataHolders.add(new PieChartView.PieceDataHolder(15, 0xFF4CCD70, "静止（15）"));
        mPieceDataHolders.add(new PieChartView.PieceDataHolder(10, 0xFFF05D75, "报警（10）"));
        pieChart.setData(mPieceDataHolders);

        tvCity.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mActivity, CityPickerActivity.class);
                startActivityForResult(intent, 001);
            }
        });


    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 001) {
                tvCity.setText(data.getStringExtra("picked_city"));
            }
        }
    }

    @Override
    public void setContentView(View view) {
        super.setContentView(view);
    }


    private void getLocation() {
        mLocationClient = new AMapLocationClient(getActivity());
        mLocationOption = new AMapLocationClientOption();
        //        /**
        //         * 设置定位场景，目前支持三种场景（签到、出行、运动，默认无场景）
        //         */
        //        mLocationOption.setLocationPurpose(AMapLocationClientOption.AMapLocationPurpose.SignIn);
        //        if(null != mLocationClient){
        //            mLocationClient.setLocationOption(mLocationOption);
        //            //设置场景模式后最好调用一次stop，再调用start以保证场景模式生效
        //            mLocationClient.stopLocation();
        //            mLocationClient.startLocation();
        //        }

        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //获取一次定位结果
        mLocationOption.setOnceLocation(true);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //关闭缓存机制
        mLocationOption.setLocationCacheEnable(true);
        //给定位客户端对象设置定位参数
        mLocationClient.setLocationOption(mLocationOption);
        //启动定位
        mLocationClient.startLocation();
        mLocationClient.setLocationListener(new AMapLocationListener() {
            @Override
            public void onLocationChanged(AMapLocation aMapLocation) {
                if (aMapLocation != null) {
                    if (aMapLocation.getErrorCode() == 0) {
                        city = aMapLocation.getCity();
                        tvCity.setText(city);
                        province = aMapLocation.getProvince();
                        code = aMapLocation.getCityCode();
                    } else {
                        //定位失败时，可通过ErrCode（错误码）信息来确定失败的原因，errInfo是错误信息，详见错误码表。
                        Log.e("AmapError", "location Error, ErrCode:"
                                + aMapLocation.getErrorCode() + ", errInfo:"
                                + aMapLocation.getErrorInfo());
                    }
                }
            }
        });

    }

    @OnClick({R.id.tv_search, R.id.img_news, R.id.rl_trajectory,R.id.rl_VehicleTrends})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_search:
                Bundle bundle = new Bundle();
                bundle.putInt("type", 1);
                RxActivityTool.skipActivity(mActivity, SmallCSearchActivity.class, bundle);
                break;
            case R.id.img_news:
                Bundle bundle1 = new Bundle();
                bundle1.putString("where", "CHomeFragment");
                RxActivityTool.skipActivity(mActivity, SmallCNewsActivity.class, bundle1);
                break;
            case R.id.rl_trajectory:
                RxActivityTool.skipActivity(mActivity, TrajectoryListActivity.class);
                break;
            case R.id.rl_VehicleTrends:
                RxActivityTool.skipActivity(mActivity, VehicleTrendsActivity.class);
                break;
        }
    }

    @Override
    protected void onDestroyViewLazy() {
        super.onDestroyViewLazy();
        if (mImmersionBar != null) {
            mImmersionBar.destroy();
        }
    }
}
